<?php
include("MysqlDButil.php");
class InsertDate{
	//插入一个游戏的数据，同时插入和category关联的第三方表wp_term_relationships，额外属性表wp_postmeta
	public function insert($obj){
		$dbuitl = new DButil();
		//$sql = "insert into games (name,type,date,size,introduce,flash,image) values ('".$obj['name']."','".$obj['type']."','".$obj['date']."','".$obj['size']."','".$obj['introduce']."','".$obj['flash']."','".$obj['image']."')";
		$sql = "insert into wp_posts(post_title,post_date,post_content,post_status,post_type) values ('".$obj['name']."',NOW(),'".$obj['introduce']."','publish','post')";
//		echo $sql;
		$dbuitl->exequery($sql);
		echo mysql_error();
		$insertid = $dbuitl->insert_rowid();
		$sql = "insert into wp_postmeta(post_id,meta_key,meta_value) values ('".$insertid."','GameSize','".$obj['size']."'),"."('".$insertid."','GameLink','".$obj['flash']."'),"."('".$insertid."','GamePic','".$obj['image']."'),"."('".$insertid."','GamePR','10'),"
		       ."('".$insertid."','GameDate','".$obj['date']."')";
//		echo $sql;
		$dbuitl->exequery($sql);
		echo mysql_error();
		$sql = "insert into wp_term_relationships(object_id,term_taxonomy_id) values ('".$insertid."','".$obj['type']."')";
//		echo $sql;
		$dbuitl->exequery($sql);
		echo mysql_error();
	}

	//wp数据库中插入分类设计到三张表 terms，term_taxonomy,term_taxonomy这个表中的主键用来和一个post关联，
	//因为类别和post是多对多的关系，所以涉及到第三方表wp_term_relationships
	public function insertCategory($category){
		$res = InsertDate::ExistCate($category);
		if(!$res){
			$dbuitl = new DButil();
			$sql = "insert into wp_terms(name,slug) values ('".$category."','".$category."')";
			echo $sql;
			$dbuitl->exequery($sql);
			$insertid = $dbuitl->insert_rowid();
			echo mysql_error();
			$sql = "insert into wp_term_taxonomy(term_id,taxonomy,parent) values ('".$insertid."','category','6')";
			echo $sql;
			$dbuitl->exequery($sql);
			$term_taxonomy_id = $dbuitl->insert_rowid();
			echo mysql_error();
			return $term_taxonomy_id;
		}else return $res;

	}

	//判断表中是否已经有此类别
	public function ExistCate($category){
		$dbuitl = new DButil();
		$sql = "select x.term_taxonomy_id from wp_term_taxonomy x where x.term_id = 
		(select t.term_id from wp_terms t where t.name='".$category."')";
		$res = $dbuitl->exequery($sql);
		$arrys = array();
		while ($a = mysql_fetch_array($res)){
			$arrys[]=$a;
		}
		return $arrys[0][0];
		
	}
}